const { STATUS_CODE } = require('../../constants');

/**
 *
 * @param {*} obj 要效验的对象
 * @param {*} fieldMinLength 对象中属性的长度
 * 对象中属性值存在Boolean(field) === false
 */

const fieldMinLength = 8;
const validateParamsMiddleware = async (ctx, next) => {
  const validatePaths = ['/user/register', '/user/register'];
  const { url } = ctx.request;
  let error;
  if (validatePaths.some(path => path === url)) {
    const { body } = ctx.request;
    Object.keys(body).forEach(item => {
      if (!body[item] || body[item] === ' ') {
        error = STATUS_CODE.PARAMS_ERROR;
        return;
      }
    });
  }
  if (error) {
    ctx.app.emit('error', new Error(error), ctx);
    return;
  }
  await next();
};

module.exports = validateParamsMiddleware;
